import { useState, useEffect } from "react";
import { Form, Select, message } from "antd";
import { CommonModal } from "@/views/components/CommonModal";
import { CommonForm } from "@/pages/OrderModule/common/components/CommonForm";
import { SKU_RETURN_DIRECTION_STATUS } from "@/views/utils/constant";
import { setReturnDestination } from "@/pages/GoodsModule/systemGoods/service";
import CommonSelect from "@/pages/OrderModule/common/components/CommonSelect";
const ReturnDestinationModal = props => {
	const { selectedRows, onCancel, fetchList } = props;
	const [form] = Form.useForm();

	const handleOk = () => {
		form.submit();
	};
	const onFinish = () => {
		const returnDestination = form.getFieldsValue().returnDestination;
		const API_DATA = selectedRows.map(item => {
			return {
				returnDestination, // 退货去向(1 销退入库 2 退货供应商)
				skuId: item.id,
				supplierId: item?.supplierId || null, // 供应商商品id
				supplierSkuId: item?.supplierSkuId || null, // 供应商商品id
				type: item.matchType // 退货类型 1 匹配供应商 2 sku
			};
		});
		setReturnDestination({ returnDestination: API_DATA }).then(res => {
			const { success, data } = res;
			// 返回的 data 是更新的条数
			if (success) {
				message.success(`操作完成，更新${data}条数据`);
				onCancel();
				fetchList();
			}
		});
	};
	return (
		<CommonModal title={{ title: "请选择退货去向" }} open={true} width={528} onCancel={onCancel} onOk={handleOk}>
			<CommonForm
				formProps={{
					form,
					initialValues: {},
					onFinish
				}}
				columns={[
					{
						name: "returnDestination",
						Component: CommonSelect,
						rules: [{ required: true, message: "请选择退货去向" }],
						props: {
							options: SKU_RETURN_DIRECTION_STATUS,
							placeholder: "必选，选择退货去向"
						}
					}
				]}
			/>
		</CommonModal>
	);
};
export default ReturnDestinationModal;
